
/*
 * JFramePrincipal.java
 *
 * Created on 23/09/2011, 20:22:55
 */

package pratica05.view;

/**
 *
 * @author elias
 */
public class JFramePrincipal extends javax.swing.JFrame implements pratica05.model.Observer {

    private pratica05.model.Component carro;
    private boolean carroCriado = false;
    
    /** Creates new form JFramePrincipal */
    public JFramePrincipal() {
        initComponents();
        setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Criação de Carros");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jLabel2.setText("Nome:");

        jLabel3.setText("Preço:");

        jTextField1.setMaximumSize(new java.awt.Dimension(30, 90));
        jTextField1.setMinimumSize(new java.awt.Dimension(10, 30));
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jTextField2.setMaximumSize(new java.awt.Dimension(30, 90));
        jTextField2.setMinimumSize(new java.awt.Dimension(10, 30));
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jButton1.setLabel("Salvar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Adicionar Aerofólio");
        jButton2.setEnabled(false);
        jButton2.setFocusPainted(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Imprime Dados");
        jButton3.setEnabled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Adicionar Escapamento");
        jButton4.setEnabled(false);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(134, 134, 134)
                .addComponent(jLabel1)
                .addContainerGap(152, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton4)
                    .addComponent(jButton3)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if ( !this.jTextField1.getText().isEmpty() && ! this.jTextField2.getText().isEmpty() ){
            try{
                carroCriado = true;
                carro = pratica05.control.Controle.criaCarro(this.jTextField1.getText(), Integer.parseInt(this.jTextField2.getText()));
                carro.agregar(this);
                this.jButton2.setEnabled(carroCriado);
                this.jButton3.setEnabled(carroCriado);
                this.jButton4.setEnabled(carroCriado);
                //JOptionPane.showMessageDialog(this, this.carro.getDescricao()+"\nPreço final: "+this.carro.getPreco(), "INformações", JOptionPane.INFORMATION_MESSAGE);
            }catch(Exception ex){
                javax.swing.JOptionPane.showMessageDialog(this, "Insira um valor válido!", "Erro!", javax.swing.JOptionPane.ERROR_MESSAGE);
            }
        }else{
            javax.swing.JOptionPane.showMessageDialog(null, "Faltando parâmetros, tente novamente", "Falta dados", javax.swing.JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        this.jButton1ActionPerformed(evt);
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        this.jButton1ActionPerformed(evt);
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if ( !this.jTextField1.getText().isEmpty() && ! this.jTextField2.getText().isEmpty() ){
            try{
                carro = pratica05.control.Controle.adicionaAeorofolio(carro, this.jTextField1.getText(), Integer.parseInt(this.jTextField2.getText()));
                carro.agregar(this);
                //JOptionPane.showMessageDialog(this, this.carro.getDescricao()+"\nPreço final: "+this.carro.getPreco(), "INformações", JOptionPane.INFORMATION_MESSAGE);
            }catch(Exception ex){
                javax.swing.JOptionPane.showMessageDialog(this, "Insira um valor válido!", "Erro!", javax.swing.JOptionPane.ERROR_MESSAGE);
            }
        }else{
            javax.swing.JOptionPane.showMessageDialog(null, "Faltando parâmetros, tente novamente", "Falta dados", javax.swing.JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        javax.swing.JOptionPane.showMessageDialog(this, this.carro.getDescricao()+"\nPreço final: "+this.carro.getPreco(), "INformações", javax.swing.JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if ( !this.jTextField1.getText().isEmpty() && ! this.jTextField2.getText().isEmpty() ){
            try{
                carro = pratica05.control.Controle.adicionaEscapamento(carro, this.jTextField1.getText(), Integer.parseInt(this.jTextField2.getText()));
                carro.agregar(this);
            }catch(Exception ex){
                javax.swing.JOptionPane.showMessageDialog(this, "Insira um valor válido!", "Erro!", javax.swing.JOptionPane.ERROR_MESSAGE);
            }
        }else{
            javax.swing.JOptionPane.showMessageDialog(null, "Faltando parâmetros, tente novamente", "Falta dados", javax.swing.JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    @Override
    public void update(pratica05.model.Component o) {
        javax.swing.JOptionPane.showMessageDialog(this, o.getDescricao()+"\nPreço final: "+o.getPreco(), "Informações", javax.swing.JOptionPane.INFORMATION_MESSAGE);
    }
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        //java.awt.EventQueue.invokeLater(new Runnable() {
         //   public void run() {
                new JFramePrincipal();//.setVisible(true);
          //  }
        //});
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
